package com.sinodata.bsm.cicp.collector.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.DBConnectionUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class MySQLUtil {
    private static Logger logger = Logger.getLogger(MySQLUtil.class);

    private MySQLUtil() {
    }

    /**
     * 获取MySQL的主版本号，默认主版本号为5
     *
     * @param db
     * @param ip
     * @param serverName
     * @return
     */
    public static int currentVersion(String db, String ip, String serverName) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String version = "5";
        try {
            conn = DBConnectionUtil.getInstance().getConnection(db, ip, serverName);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(MySQLContants.VERSION);
            rs.next();
            String ver = rs.getString(1);
            if (ver != null && ver.length() > 0 && !"".equals(ver.trim())) {
                version = ver.trim().substring(0, 1);
            }
        } catch (Exception ex) {
            logger.error("Failed to get the major version of MySQL!", ex);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            DBConnectionUtil.getInstance().returnConnection(conn);
        }
        return Integer.parseInt(version);
    }

    /**
     * 初始化要查询数据的键值
     *
     * @param collector
     */
    public static void initDbKeyMap(String[] servKey, String[] propMeta, Map<String, String> dbKeyMap) {
        for (int i = 0; i < propMeta.length; i++) {
            dbKeyMap.put(propMeta[i], servKey[i].toLowerCase());
        }
    }

    /**
     * @param dbMap
     * @return
     */
    public static List<ResPropertyValue> createPropValues(Map<String, String> dbKeyMap, String[] propMeta, Map<String, String> dbMap) {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMeta.length];
        String dbKey = null;
        for (int i = 0; i < propMeta.length; i++) {
            dbKey = dbKeyMap.get(propMeta[i]);
            propValues[i] = ResPropertyValue.createPropValue((!dbMap.containsKey(dbKey) ? "N/A" : dbMap.get(dbKey)), propMeta[i]);
        }

        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    public static void getResPropertyValueXList(String[] propMetas, List<ResPropertyValue> values) {
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        for (int i = 0; i < propValues.length; i++) {
            propValues[i] = ResPropertyValue.createPropValue("N/A", propMetas[i]);
        }
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
    }

    public static boolean isVersion4(String dbType, String ip, String serverName) {
        if (!"mysql".equals(dbType)) {
            return false;
        }
        int version = MySQLUtil.currentVersion(dbType, ip, serverName);
        if (version == 4) {
            return true;
        }
        return false;
    }
}
